package org.agilelang.generator.javastruts2;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.util.List;
import org.agilelang.mvc.Facade;
import org.agilelang.object.Controller;

public class IJavaStruts2 extends Facade {

    String tpl = "";
    /*public IJavaStruts2(String path_source, String path_app) {
     this.path_source = path_source;
     this.path_app = path_app;
     this.process();
     }*/

    public IJavaStruts2(String path_source, String path_app) {
        this.path_xml = path_source;
        this.path_app = path_app;
        this.tpl ="clasic";
        //scanner_parser(new JTextArea());
        readProject();
    }

    public void processFile() {
        GFile gfile = new GFile(path_app, project);
        gfile.setTemplate(tpl);
        gfile.process();

    }

    public void processController() {
        List<Controller> controllers = project.controllers;
        int t = controllers.size();
        for (int i = 0; i < t; i++) {
            Controller controller = controllers.get(i);
            if (controller.type.equals("simple")) {
                GControllerSimple generate_controller = new GControllerSimple(path_app, project, controller);
                generate_controller.setTemplate(tpl);
                generate_controller.process();
            } else if (controller.type.equals("scaffold")) {
                GControllerScaffold generate_controller = new GControllerScaffold(path_app, project, controller);
                generate_controller.setTemplate(tpl);
                generate_controller.process();
            } else if (controller.type.equals("transaction")) {
                GControllerTransaction generate_controller = new GControllerTransaction(path_app, project, controller);
                generate_controller.setTemplate(tpl);
                generate_controller.process();
            } else {
                //error
            }
        }



        //GControllerSimple generate_controller = new GControllerSimple(path_app, project);
        //generate_controller.process();
        //GenerateController generate_controller = new GenerateController(project.application.name, path_app, project.controllers, project.models, project.scaffolds);
        //generate_controller.process();
    }

    public void processModel() {
        if (!project.models.isEmpty()) {
            GModel generate_model = new GModel(path_app, project);
            generate_model.setTemplate(tpl);
            generate_model.process();
        }

    }

    public void processMenu() {
        GMenu menu = new GMenu(path_app, project);
        menu.setTemplate(tpl);
        menu.process();

        /*GenerateStrutsXML struts = new GenerateStrutsXML(path_app, project.controllers, project.scaffolds);
         struts.process();
         */


        //GFile gfile = new GFile(path_app, project);

        //gfile.menu(project.menus, project.application.name, project.application.description);
        //gfile.database(project.application.host, project.application.login, project.application.password, project.application.database);

    }

    public void processApp() {
        processFile();
        processController();
        processModel();

        processMenu();

        /*
         String path_sql = (new File(editor.getPathFile())).getParentFile().getAbsolutePath(); 
         if (struts2.getApp().source.equals("mysql")) { 
         GMySql gmysql = new
         * GMySql(); gmysql.setModelList(struts2.getProject().models);
         * gmysql.setScaffoldList(struts2.getProject().scaffolds);
         * gmysql.writeFileSql(path_sql);
         *
         * } else if (struts2.getApp().source.equals("mysql")) { GPostgres
         * gpostgres = new GPostgres();
         * gpostgres.setModelList(struts2.getProject().models);
         * gpostgres.setScaffoldList(struts2.getProject().scaffolds);
         * gpostgres.writeFileSql(path_sql); } else { //error } String path_doc
         * = (new File(editor.getPathFile())).getParentFile().getAbsolutePath()
         * + "/doc.doc"; GeneratorDoc doc = new GeneratorDoc(path_doc,
         * struts2.getProject());
         */
    }

    public void setTemplate(String clasic) {
        tpl = clasic;
    }
}
